<?php

namespace app\api\controller;

use app\api\logic\UserCollectionLogic;
use app\basic\ApiController;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("收藏接口")
 */
class CollectionController extends ApiController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new UserCollectionLogic();
        parent::__construct();
    }

    /**
     * @Apidoc\Title("获取收藏统计")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("month_count", type="int", desc="本月收藏"),
     *     @Apidoc\Returned("total_count", type="int", desc="总收藏数")
     * })
     */
    public function getCount()
    {
        $userId = $this->auth->getUserId();
        $data = $this->logic->getCollectionCount($userId);
        return $this->success($data);
    }

    /**
     * @Apidoc\Title("获取收藏列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("list", type="array", desc="收藏列表", children={
     *         @Apidoc\Returned("id", type="int", desc="收藏ID"),
     *         @Apidoc\Returned("article_id", type="int", desc="文章ID"),
     *         @Apidoc\Returned("title", type="string", desc="标题"),
     *         @Apidoc\Returned("create_time", type="string", desc="收藏时间"),
     *     }),
     *     @Apidoc\Returned("total", type="int", desc="总记录数"),
     *     @Apidoc\Returned("page", type="int", desc="当前页码"),
     *     @Apidoc\Returned("limit", type="int", desc="每页数量")
     * })
     */
    public function getList()
    {
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 10);
        
        $list = $this->logic->getCollectionList($this->auth->getUserId(), $page, $limit);
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("批量删除收藏")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("ids", type="string", require=true, desc="收藏ID，多个以逗号分隔，如：8,9")
     */
    public function batchDelete()
    {
        $ids = $this->request->post('ids', '');
        $idArray = array_filter(explode(',', $ids), function($id) {
            return is_numeric($id) && $id > 0;
        });
        
        if (empty($idArray)) {
            return $this->fail('参数错误');
        }
        $result = $this->logic->deleteCollection($this->auth->getUserId(), $idArray);
        return $result ? $this->success('删除成功') : $this->fail('删除失败');
    }

    /**
     * @Apidoc\Title("取消收藏")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("ids", type="string", require=true, desc="收藏ID，多个以逗号分隔，如：8,9")
     */
    public function delete()
    {
        $ids = $this->request->post('ids', '');
        $result = $this->logic->deleteCollectionId($this->auth->getUserId(), $ids);
        return $result ? $this->success('删除成功') : $this->fail('删除失败');
    }




    /**
     * @Apidoc\Title("添加收藏")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("article_id", type="int", require=true, desc="文章ID")
     * @Apidoc\Param("title", type="string", require=true, desc="文章标题")
     */
    public function add()
    {
        $articleId = $this->request->post('article_id', 0);
        $title = $this->request->post('title', '');
        
        if (empty($articleId) || empty($title)) {
            return $this->fail('参数错误');
        }
        // 检查是否已收藏
        if ($this->logic->isCollected($this->auth->getUserId(), $articleId)) {
            return $this->fail('已经收藏过了');
        }
        
        $result = $this->logic->addCollection(
            $this->auth->getUserId(),
            $articleId,
            $title
        );
        
        return $result ? $this->success('收藏成功') : $this->fail('收藏失败');
    }
} 